// pages/classification/index.js
const common = require('../../../../public.js')
const utils = require('../utils.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeTab: 0,
    tabsList: [],
    contentList: [],
    loadingSuccess: false,
    lanFlag:"",
    tabs:'managerId',
    active:1
  },
  
 onChangeTabbars(e) { //切换底部导航
   console.log(e)
   const index = e.detail.index
   const url = e.detail.url + '?fl=lanling' 
   console.log(url)
   if (index != this.data.active) {
     utils.onChangeTabbar(url, this.data.storeId, this.data.moduleType)
   }
 },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      lanFlag:options.fl
    })
  },
  onShow() {
    if (this.data.loadingSuccess === false) {
      this.getAllCategory()
    }
  },
  getAllCategory() {
    let that = this
    let tempJson = {}
    //进行判断看是哪一个程序兰陵还是飞羊
    var url
    if(that.data.lanFlag=='lanling'){
       url = '/getApplyStoreCatList'
    } else{
      url = '/getApplyStoreCatList'
    }
    common.post(url, tempJson, function (res) {
      if (res.status === 200) {
        that.setData({
          tabsList: res.data
        }, _ => {
          that.getChildCategory(res.data[0].id)
        })
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  getChildCategory(cid) {
    const that = this
    const tempJson = {
      cid
    }
    common.post('/getAllItemCatList', tempJson, function (res) {
      if (res.status === 200) {
        that.setData({
          contentList: res.data,
          loadingSuccess: true
        })
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  clickCategoryType(e) {
    let {
      index
    } = e.currentTarget.dataset
    let cid = this.data.tabsList[index].id
    this.setData({
      activeTab: index
    }, _ => {
      this.getChildCategory(cid)
    })
  },
  toGoodsCategory(e) {
    let {
      item
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/Dj/classification/classificationGoods/index?cid=' + item.id,
    })
  }
})